import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import commonjs from "rollup-plugin-commonjs";
import externalGlobals from "rollup-plugin-external-globals";
const path = require("path");

export default defineConfig(({ command, mode }) => {
  let commonConfig = {
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
        "@comps": path.resolve(__dirname, "./src/components"),
      },
      extensions: [".vue", ".ts"],
    },
    define: {
      "process.env": {},
    },
    plugins: [vue()],
  };

  let modeConfig = {};

  if (command === "serve") {
    modeConfig = {
      server: {
        host: "127.0.0.1",
        port: 8088,
        open: true,
        strictPort: false,
        https: false,
        proxy: {
          "/api":{
            target:"http://localhost:3030/api"
          }
        },
      },
    };
  } else {
    modeConfig = {
      build: {
        rollupOptions: {
          external: ["vue", "element-plus"],
          output: {
            globals: {
              vue: "Vue",
              "element-plus": "ElementPlus"
            }
          },
          plugins: [
            commonjs(),
            externalGlobals({
              vue: "Vue",
              "element-plus": "ElementPlus"
            })
          ]
        },
      },
    };
  }

  return Object.assign({}, commonConfig, modeConfig);
});
